//
//  SixteenViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/3/9.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class SixteenViewController: UIViewController {

    fileprivate let cellId = "sixteen_cell";
    fileprivate lazy var mainTableView:UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: screenMainSize.height), style: UITableView.Style.plain);
        tableView.backgroundColor = .white;
        tableView.showsVerticalScrollIndicator = false;
        tableView.showsHorizontalScrollIndicator = false;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId);
        tableView.sectionIndexColor = .purple;//索引颜色
        return tableView;
    }();
    
    fileprivate var dataArray:NSMutableArray = {
        var mutArr = NSMutableArray();
        
        mutArr.add("表格索引和删除");
        mutArr.add("搜索栏");
        
        return mutArr;
    }();
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "UITableView";
        // Do any additional setup after loading the view.
        self.onCreatUIForNivation();
        self.onCreatUIForView();
    }
    
    //TODO:UI
    fileprivate func onCreatUIForNivation() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .done, target: self, action: #selector(onBackAction));
    }
    
    fileprivate func onCreatUIForView() {
        self.view.backgroundColor = .white;
        
        self.view.addSubview(self.mainTableView);
    }

    //TODO:Actions
    @objc fileprivate func onBackAction() {
        self.navigationController?.dismiss(animated: true, completion: nil);
    }
}

extension SixteenViewController:UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId)!;
        cell.backgroundColor = .lightGray;
        
        if self.dataArray.count > indexPath.section {
            cell.textLabel?.text = (self.dataArray[indexPath.row] as! String);
        }
        
        return cell;
    }
}

extension SixteenViewController:UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.row {
            switch indexPath.row {
            case 0:
                let vc = SixteenIndexTitleViewController();
                self.navigationController?.pushViewController(vc, animated: true);
                return;
            case 1:
                let vc = SixteenSearchViewController();
                self.navigationController?.pushViewController(vc, animated: true);
                return;
            default:
                ""
            }
        }
    }
}
